#import "DDRequest.h"
@interface DDRequest()
@property (nonatomic, strong) NSMutableDictionary *para;
@property (nonatomic, strong) NSMutableData *data;
@property (nonatomic, strong) loginBlock login_block;
@end
@implementation DDRequest
- (NSMutableDictionary *)para
{
    if (!_para)
    {
        _para = [NSMutableDictionary dictionaryWithDictionary:helpermanger().getPara().localInfo];
    }
    return _para;
}
- (NSMutableData *)data
{
    if (!_data)
    {
        _data = [NSMutableData data];
    }
    return _data;
}
- (void)yjd_YJ_registerRequst
{
    self.type = JTWRequestTypeYJ_Register;
    [self.para setValue:@"" forKey:@"account"];
    [self.para setValue:@"" forKey:@"pass"];
    [self yjd_request];
}
- (void)yjd_visitorRequest
{
    self.type = JTWRequestTypeVisitor;
    [self.para setValue:@"" forKey:@"account"];
    [self.para setValue:@"" forKey:@"pass"];
    [self yjd_request];
}
- (void)yjd_registerRequst:(NSDictionary *)dict
{
    self.type = JTWRequestTypeRegister;
    [self.para setValue:[dict valueForKey:@"name"] forKey:@"account"];
    [self.para setValue:[[dict valueForKey:@"password"] yjd_md5] forKey:@"pass"];
    [self yjd_request];
}
- (void)yjd_loginRequest:(NSDictionary *)dict
{
    self.type = JTWRequestTypeLogin;
    [self.para setValue:[dict valueForKey:@"name"] forKey:@"account"];
    [self.para setValue:[[dict valueForKey:@"password"] yjd_md5] forKey:@"pass"];
    [self yjd_request];
}
- (void)yjd_loginRequestWithBlock:(loginBlock)block
{
    self.type = JTWRequestTypeLogin;
    self.login_block = block;
    [self.para setValue:@"" forKey:@"account"];
    [self.para setValue:@"" forKey:@"pass"];
    [self yjd_request];
    
}
- (void)yjd_authenticateRequest:(NSDictionary *)dict;
{
    self.type = JTWRequestTypeAuthenticate;
    [self.para removeAllObjects];
    [self.para setDictionary:dict];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self.para setValue:helpermanger().getPara().appID forKey:@"appid"];
    [self yjd_request];
}
- (void)yjd_verifyCodeRequest:(NSDictionary *)dict
{
    self.type = JTWRequestTypeVerifyCode;
    [self.para removeAllObjects];
    [self.para setDictionary:dict];
    [self.para setValue:helpermanger().getPara().appID forKey:@"appid"];
    [self yjd_request];
}
- (void)yjd_resetPWDRequest:(NSDictionary *)dict
{
    [self.para removeAllObjects];
    self.type = JTWRequestTypeResetPWD;
    [self.para setValue:helpermanger().getPara().appID forKey:@"appid"];
    [self.para setValue:[dict valueForKey:@"account"] forKey:@"account"];
    [self.para setValue:[[dict valueForKey:@"pass"] yjd_md5] forKey:@"pass"];
    [self.para setValue:[dict valueForKey:@"phone"] forKey:@"phone"];
    [self yjd_request];
}
- (void)yjd_alterPWDRequest:(NSDictionary *)dict
{
    [self.para removeAllObjects];
    self.type = JTWRequestTypeAlterPWD;
    [self.para setValue:helpermanger().getPara().appID forKey:@"appid"];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self.para setValue:[[dict valueForKey:@"oldpassword"] yjd_md5] forKey:@"pass"];
    [self.para setValue:[[dict valueForKey:@"password"] yjd_md5] forKey:@"password"];
    [self yjd_request];
}
- (void)yjd_bindRequest:(NSDictionary *)dict
{
    self.type = JTWRequestTypeBind;
    [self.para removeAllObjects];
    [self.para setDictionary:dict];
    [self.para setValue:helpermanger().getPara().appID forKey:@"appid"];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self yjd_request];
}
- (void)yjd_prepareZFRequest:(DDProductPara )product
{
    self.para = nil;
    self.type = JTWRequestTypePrepareZF;
    [self.para insertDict:propertyDic(product)];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self.para setValue:helpermanger().getPara().gameAccountID forKey:@"gameaccountid"];
    [self yjd_request];
}
- (void)yjd_zfRequest:(NSDictionary * )product
{
    self.para = nil;
    self.type = JTWRequestTypeZF;
    [self.para insertDict:product];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self.para setValue:helpermanger().getPara().gameAccountID forKey:@"gameaccountid"];
    [self yjd_request];
}
void yjd_zfRequest(DDProductPara product)
{
    
}
- (void)yjd_exchangeRequest:(NSDictionary *)dict
{
    self.para = nil;
    self.type = JTWRequestTypeExchange;
    [self.para insertDict:dict];
    [self.para setValue:helpermanger().getPara().accountID forKey:@"accountid"];
    [self yjd_request];
}
- (void)yjd_request
{
    NSMutableDictionary *requestPara = [NSMutableDictionary dictionary];
    NSString *paraString = [self.para json];
    [requestPara setValue:paraString forKey:@"transdata"];
    NSString *sign = [paraString yjd_sign];
    [requestPara setValue:sign forKey:@"sign"];
    [requestPara setValue:@"RSA" forKey:@"signtype"];
    [CCHttp http].basicURLString = helpermanger().getPara().serviceHeader;
    [self.task cancel];
    self.data = nil;
    NSString *api = @"";
    if (self.type == JTWRequestTypeLogin)
        api = LOGIN_API;
    else if (self.type == JTWRequestTypeRegister || self.type == JTWRequestTypeQuickRegister || self.type == JTWRequestTypeYJ_Register || self.type == JTWRequestTypeVisitor)
        api = REGISTE_API;
    else if (self.type == JTWRequestTypeAuthenticate)
        api = AUTHENTICATE_API;
    else if (self.type == JTWRequestTypeVerifyCode)
        api = VERIFYCODE_API;
    else if (self.type == JTWRequestTypeResetPWD)
        api = RESETPASSWORD_API;
    else if (self.type == JTWRequestTypeAlterPWD)
        api = ALTERPASSWORD_API;
    else if (self.type == JTWRequestTypeBind)
        api = BIND_API;
    else if (self.type == JTWRequestTypePrepareZF)
        api = PREPARE_ZF_API;
    else if (self.type == JTWRequestTypeZF)
        api = ZF_API;
    else if (self.type == JTWRequestTypeExchange)
        api = EXCHANGE_API;
    self.task = [CCHttp post:api para:requestPara success:^(id  _Nonnull response) {
        NSDictionary *dict = (NSDictionary *)response;
        if (dict)
        {
            NSString *sign = [dict valueForKey:@"sign"];
            NSString *transdata = [dict valueForKey:@"transdata"];
            BOOL isSuccess = [transdata yjd_verifySign:sign];
            if (isSuccess)
            {
                NSDictionary *trans = [NSJSONSerialization JSONObjectWithData:[transdata dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
                if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_didRequestWithReceiveData:error:)])
                {
                    [self.delegate yjd_didRequestWithReceiveData:trans error:nil];
                }
                else if (self.delegate == nil && self.login_block)
                {
                    self.login_block(trans);
                }
            }
            else
            {
                DeLog(@"验证失败");
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_didRequestWithReceiveData:error:)])
                    {
                        [self.delegate yjd_didRequestWithReceiveData:nil error:nil];
                    }
                    else if (self.delegate == nil && self.login_block)
                    {
                        self.login_block(nil);
                    }
                });
            }
        }
    } fail:^(NSError * _Nonnull error) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(yjd_didRequestWithReceiveData:error:)])
        {
            [self.delegate yjd_didRequestWithReceiveData:nil error:error];
        }
        else if (self.delegate == nil && self.login_block)
        {
            self.login_block(nil);
        }
    }];
}
- (void)dealloc
{
    [self.task cancel];
    self.task = nil;
}

@end
